<template>
  <div class="box">
    <header class="header">
      <van-nav-bar title="账号登录" left-arrow @click-left="$router.back()" />
    </header>
    <div class="content">
      <van-form @submit="onSubmit">
        <van-cell-group inset>
          <van-field
            v-model="username"
            name="username"
            label="用户名"
            placeholder="用户名/邮箱/手机号"
            :rules="[{ required: true, message: '请填写用户名' }]"
          />
          <van-field
            v-model="password"
            type="password"
            name="password"
            label="密码"
            placeholder="密码"
            :rules="[{ required: true, message: '请填写密码' }]"
          />
        </van-cell-group>
        <div style="margin: 16px;">
          <van-button round block type="primary" native-type="submit">登录</van-button>
        </div>
      </van-form>
      <div>
        <p style="float:right;margin:20px" @click="$router.push('/register')">快速注册</p>
      </div>
    </div>
  </div>
</template>
<script setup>
import request from "@/utlis/request.js";
import { showSuccessToast, showFailToast } from 'vant';
import md5 from "blueimp-md5"
import { useRouter } from 'vue-router'
const router = useRouter()
import { ref } from "vue";
const username = ref("");
const password = ref("");
const onSubmit = values => {
  console.log("submit", values);
  request.post('/login',{
    username:values.username,
    password:md5(password.value)
  }).then((res) => {
    if(res.data.code==200)
    {
        //提示登录成功
        showSuccessToast(res.data.message);
        //存储token和username
        localStorage.setItem('token',res.data.token)
        localStorage.setItem('username',username.value)
        //跳转到首页
        router.push('/')
    }
    else
    {
        showFailToast(res.data.message);
    }
  })
};
</script>